<template>
  <a-modal
          title="选择"
          :width="800"
          :visible="visible"
          :confirmLoading="confirmLoading"
          :destroyOnClose="true"
          @ok="handleSubmit"
          @cancel="handleCancel"
          cancelText="关闭"
  >
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="12">
          <a-col :md="6" :sm="8">
            <a-form-item
              label="姓名"
              :maxLength="100">
              <a-input placeholder="请输入姓名" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="querys" icon="search">查询</a-button>
              <a-button
                @click="() => {this.$objToReset(queryParam),this.querys()}"
                icon="reload"
                style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
      <a-table
              row-key="id"
              size="middle"
              :columns="columns"
              :dataSource="dataSource"
              :pagination="true"
              :row-selection="{ columnWidth:10,selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <template slot="action" slot-scope="text, record">

        </template>
      </a-table>
    </div>
  </a-modal>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { deleteAction, postAction } from '@/api/manage'
import { mixin } from '@/utils/mixin'
import WxFilterDepart from '@/components/WxFilterDepart/WxFilterDepart'
import store from '@/store'
import UserEdit from "./UserEdit";
import FileUpload from "./fileUpload";

export default {
  name: 'UserListSelect',
  mixins: [mixin],
  components: {
      FileUpload,
      UserEdit,
    STable,
    Ellipsis,
    WxFilterDepart
  },
  data () {
    return {
      visible: false,
      // 查询条件
      queryParam: {
        mobile: '',
        timeinfo: [],
        times: '',
        idcard: '',
        name: ''
      },
      // 表头
      // 如果需要字典，那么需要在columns列中加入 customRender: (text) => this.$sysDictFilter(text, 'xxxx')xxxx 为dataIndex
      columns: [
          {
              title: '名称',
              dataIndex: 'name',
              customRender: (text, record) => (text || '-')
          },
          {
              title: '角色',
              dataIndex: 'role',
              customRender: (text, record) =>   this.$sysDictFilter(text, 'roles')
          },
        {
          title: '手机号',
          dataIndex: 'mobile',
          customRender: (text, record) => (text || '-')
        },
          {
              title: '所属部门',
              dataIndex: 'departCode',
              customRender: (text, record) => this.$getDepartmentName(text)
          },
          {
              title: '厂家',
              dataIndex: 'factorName'
          },
        {
          title: '创建时间',
          dataIndex: 'createTime'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        console.log('loadData.parameter', parameter)
        return postAction(this.url.list, Object.assign(parameter, this.queryParam))
            .then(res => {
              return res.result
            })
      },
        dataSource:[],
        wid:undefined,
      selectedRowKeys: [],
      options: {
        alert: {
          clear: () => {
            this.selectedRowKeys = []
          }
        },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange,
          type: 'checkbox'
        }
      },
      url: {
        list: '/system/user/list',
        delete: '/system/user/delete',
          dels: '/system/user/dels',
          sendInfo: '/system/hosWorkInfo/sendInfo',
      }
    }
  },
  computed: {},
  methods: {
      show (record) {
          this.wid=record
          this.queryParam={};
          const that = this
          postAction(this.url.list, Object.assign({"pageNo":1,"pageSize":1000}, this.queryParam))
              .then(res => {
                  that.dataSource = res.result.data.records
              })

          this.visible = true
      },
      querys(){
          const that = this
          postAction(this.url.list, Object.assign({"pageNo":1,"pageSize":1000}, this.queryParam))
              .then(res => {
                  that.dataSource = res.result.data.records
              })
      },
      onSelectChange (selectedRowKeys) {
          console.log('selectedRowKeys changed: ', selectedRowKeys)
          this.selectedRowKeys = selectedRowKeys
      },
      // 关闭
      handleCancel () {
          this.$emit('ok')
          this.close()
      },
      close () {
          this.$emit('ok')
          this.selectedRowKeys = []
          this.visible = false
      },
      // 确定
      handleSubmit () {
          if(this.selectedRowKeys.length==0){
              this.$message.error('请选择要分享的人!')
              return ;
          }
          const keys = this.selectedRowKeys.join(',');
          // 触发表单验证
          this.confirmLoading = true

          postAction(this.url.sendInfo, {"id":this.wid,"userids":keys}).then(res => {
              this.$message.success(res.content)
              this.$emit('ok', this.formInfo)
              this.close()
          }).finally(() => {
              this.confirmLoading = false
          })

      },
    // 图像处理
    getAvatarView: function (avatar) {
      return this.$getFileAccessHttpUrl(avatar)
    },
    // 用户编辑
    handleEdit (record) {
      this.$refs.modalEdit.edit(record)
    },
    searchQuery () {
      this.$refs.table.refresh(true)
    },
    // 保存刷新
    modalEditOk () {
      this.$refs.table.refresh()
    },
    onChange (value, dateString) {
      console.log(dateString[0], dateString[1])
      this.queryParam.times = dateString[0] + '~' + dateString[1]
    },
  },
  watch: {},
  activated () {
    if (store.getters.checkKeepAliveTmpList(this.$options.name)) {
      this.$refs.table.refresh(true)
    }
  },
  beforeRouteLeave (to, from, next) {
    if (!(to.name === 'userCashRecord' || to.name === 'userBalanceRecord')) {
      store.commit('noKeepAlive', this.$options.name)
    }
    next()
  },
  beforeRouteEnter (to, from, next) {
    if ((from.name === 'userCashRecord' || from.name === 'userBalanceRecord')) {
      store.commit('iskeepAliveTmp', 'UserList')
    }
    store.commit('iskeepAlive', 'UserList')
    next()
  }
}
</script>
<style scoped>
</style>
